/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ing.sisval.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.ing.sisval.be.BEPersona;
import org.rx.cr.dao.DAOAbstract;

/**
 *
 * @author Mysham
 */
public class DAPersona extends DAOAbstract<BEPersona> {

    public DAPersona(Connection conn) {
        setConnection(conn);
    }

    @Override
    public int registrar(BEPersona be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_ins_persona");
            setParameterString(be.getNombres());
            setParameterString(be.getAppaterno());
            setParameterString(be.getApmaterno());
            setParameterString(be.getDni());
            setParameterString(be.getClave());
            setParameterString(be.getEstadocuenta());
            return Integer.parseInt(commit().toString());          

        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BEPersona be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_upd_persona");
            setParameterInt(be.getPersonaid());
            setParameterString(be.getNombres());
            setParameterString(be.getAppaterno());
            setParameterString(be.getApmaterno());
            setParameterString(be.getDni());
            setParameterString(be.getClave());
            setParameterString(be.getEstadocuenta());
            return Integer.parseInt(commit().toString());          

        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BEPersona be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_del_persona");
            setParameterInt(be.getPersonaid());
            
            return Integer.parseInt(commit().toString());          

        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BEPersona> buscarReferencia(String ref) throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_persona");
            setParameterString(ref);
            commitc();
            while (existResult()) {                
                BEPersona be= new BEPersona();
                be.setPersonaid(getDataInt(1));
                be.setNombres(getDataString(2));
                be.setAppaterno(getDataString(3));
                be.setApmaterno(getDataString(4));
                be.setDni(getDataString(5));
                be.setClave(getDataString(6));
                be.setEstadocuenta(getDataString(7));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BEPersona> listar() throws Exception {
         try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_persona");
            setParameterString("");
            commitc();
            while (existResult()) {                
                BEPersona be= new BEPersona();
                be.setPersonaid(getDataInt(1));
                be.setNombres(getDataString(2));
                be.setAppaterno(getDataString(3));
                be.setApmaterno(getDataString(4));
                be.setDni(getDataString(5));
                be.setClave(getDataString(6));
                be.setEstadocuenta(getDataString(7));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BEPersona buscar(int i) throws Exception {
         try {
             BEPersona be= null;
            setStoreProcedure("SISVAL.sp_sel_persona_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {                
               be= new BEPersona();
                be.setPersonaid(getDataInt(1));
                be.setNombres(getDataString(2));
                be.setAppaterno(getDataString(3));
                be.setApmaterno(getDataString(4));
                be.setDni(getDataString(5));
                be.setClave(getDataString(6));
                be.setEstadocuenta(getDataString(7));
                
            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }
    @Override
     public BEPersona autenticarUsuario(String user, String password) {
        try {
            BEPersona be = null;
            setStoreProcedure("sisval.sp_autenticar_usuario");
            setParameterString(user);
            setParameterString(password);
            commitc();
            while (existResult()) {                
                be = new BEPersona();
                be.setPersonaid(getDataInt(1));
                be.setNombres(getDataString(2));
                be.setAppaterno(getDataString(3));
                be.setApmaterno(getDataString(4));
                be.setDni(getDataString(5));                
                be.setClave(getDataString(6));
                be.setEstadocuenta(getDataString(7)); 
            }
            return be;
        } catch (Exception e) {
            try {
                rollback(e);                
            } catch (Exception ex) {}
            return null;
        }
    }

}
